const state = {
  rows: []
}

const mutations = {
  SET_HANNDLE_SELECTION_DELETE: (state, index) => {
    state.rows.splice(index, 1)
  },
  SET_HANNDLE_SELECTION_CLEAR: (state) => {
    state.rows = []
  },
  SET_HANNDLE_SELECTION: (state, {rows, list}) => {
    if (state.rows.length === 0) {
      state.rows = [...rows]
    } else {
      let newData = []
      let stateData = state.rows
      stateData.map(item => {
        let bl = false
        if ( list.some(handle =>
          handle.id === item.id
        )) {
          bl = true
        }
        if (!bl) {
          newData.push(item)
        }
      })
      newData = [...newData, ...rows]
      state.rows = Array.from(new Set(newData.map(JSON.stringify)), JSON.parse)
    }
  }
}

const actions = {
  handleSelection({ commit }, data) {
    commit('SET_HANNDLE_SELECTION', data)
  },
  handleSelectionClear({ commit }) {
    commit('SET_HANNDLE_SELECTION_CLEAR')
  },
  handleSelectionDelete({ commit }, index) {
    commit('SET_HANNDLE_SELECTION_DELETE', index)
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
